/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestHashMap.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package collections;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 集合框架 - java.util.HashMap
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestHashMap {

	/**
	 * 测试HashMap的方法
	 */
	@SuppressWarnings("unused")
	public static void testHashMap() {
		// 添加方法
		Map<String, String> map1 = new HashMap<String, String>();
		map1.put("key1", "value1");
		map1.put("key2", "value2");

		Map<String, String> map2 = new HashMap<String, String>();
		map2.putAll(map1);

		// 删除方法
		map1.remove("key2");
		map1.clear();

		// 查询方法
		String value1 = map2.get("key1");
		int map2Size = map2.size();
		map2.values();// 拿到Map所有的值, 返回一个Collection

		// 判断方法
		map2.containsKey("key2");
		map2.containsValue(value1);
		map1.isEmpty();
	}

	/**
	 * 遍历Map方式1
	 * 
	 * @param map
	 */
	public static void iteratorMap1(Map<String, String> map) {
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			System.out.println("key ---> " + key + " value ---> " + map.get(key));
		}
	}

	/**
	 * 遍历Map方式2
	 * 
	 * @param map
	 */
	public static void iteratorMap2(Map<String, String> map) {
		Set<Map.Entry<String, String>> entrySet = map.entrySet();
		for (Map.Entry<String, String> entry : entrySet) {
			System.out.println("key ---> " + entry.getKey() + " value ---> " + entry.getValue());
		}
	}

	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("1", "One");
		map.put("2", "Two");
		map.put("3", "Three");

		testHashMap();
		iteratorMap1(map);
		System.out.println();
		iteratorMap2(map);
	}
}
